package cn.ihelloworld.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.ihelloworld.bean.Article;
import cn.ihelloworld.bean.Catagory;
import cn.ihelloworld.bean.Tag;
import cn.ihelloworld.dao.Pagation;
import cn.ihelloworld.service.IArticleService;
import cn.ihelloworld.service.ICatagoryService;
import cn.ihelloworld.service.ITagService;
import cn.ihelloworld.util.JSONResult;

@Controller
@RequestMapping("/admin/article/")
public class ArticleController {
	@Autowired
	private IArticleService articleService;
	
	@Autowired
	private ICatagoryService catagoryService;
	@Autowired
	private ITagService tagService;
	
	@RequestMapping("index")
	public String index(){
		return "admin/article";
	}
	
	@RequestMapping("query")
	@ResponseBody
	public JSONResult<Article> query(Article article,Integer page,Integer rows){
		JSONResult<Article> json=new JSONResult<Article>();
		Pagation<Article> paga=articleService.selectForPage(article, page, rows);
		List<Article> list=paga.getRs();
		for(Article bean:list){
			bean.setCatagoryName(bean.getCatagory().getName());
			bean.setCatagory(null);
			bean.setSummary(null);
			bean.setCreateDate(null);
			bean.setContent(null);
		}
		json.setTotal(paga.getTotal());
		json.setRows(list);
		return json;
	}
	
	@RequestMapping("add")
	@ResponseBody
	public JSONResult<Article> add(Article article){
		JSONResult<Article> json=new JSONResult<Article>();
		Catagory catagory=new Catagory();
		catagory.setId(article.getCatagoryId());
		article.setReadCount(1);
		article.setCommentCount(0);
		article.setCatagory(catagory);
		articleService.saveOrUpdate(article);
		String[] tags=article.getTag().split(",");
		for(String tag:tags){
			Tag bean=new Tag();
			bean.setName(tag);
			bean.setCreateDate(new Date());
			List<Tag> list=tagService.select(bean);
			if(list.size()>0){
				bean=list.get(0);
				if(bean.getUseCount()==null){
					bean.setUseCount(1);
				}else{
					bean.setUseCount(bean.getUseCount()+1);
				}
				tagService.saveOrUpdate(bean);
//				bean.sa
			}else{
				bean.setUseCount(1);
				bean.setReadCount(1);
				tagService.saveOrUpdate(bean);
			}
		}
		return json;
	}
	
	@RequestMapping("update")
	@ResponseBody
	public JSONResult<Article> modify(String list){
		JSONResult<Article> json=new JSONResult<Article>();
		JSONArray jsonarray=JSONArray.fromObject(list);
		List<Article> articles=JSONArray.toList(jsonarray,Article.class);
		List<Catagory> cas=catagoryService.select(new Catagory());
		Map<String,String> map=new HashMap<String,String>();
		for(Catagory ca:cas){
			map.put(ca.getName(), ca.getId());
		}
		if(articles!=null){
			for(Article ar:articles){
				ar.setCatagoryId(map.get(ar.getCatagoryName()));
				ar.setCatagoryName(null);
			}
		}
		articleService.update(articles);
		json.setStatus(true);
		return json;
	}
	
	@RequestMapping("load")
	@ResponseBody
	public JSONResult<Article> load(String id){
		JSONResult<Article> json=new JSONResult<Article>();
		Article article=articleService.get(id);
		json.setBean(article);
		json.setStatus(true);
		return json;
	}
	@RequestMapping("edit")
	@ResponseBody
	public JSONResult<Article> edit(Article article){
		JSONResult<Article> json=new JSONResult<Article>();
		articleService.saveOrUpdate(article);
		json.setStatus(true);
		return json;
	}
	
	@RequestMapping("delete")
	@ResponseBody
	public JSONResult<Article> delete(String ids){
		JSONResult<Article> json=new JSONResult<Article>();
		String[] array=ids.split(",");
		articleService.delete(array);
		json.setStatus(true);
		return json;
	}
}
